var express = require('express')
var path = require('path')
var favicon = require('serve-favicon')
var logger = require('morgan')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var session = require('express-session')
var RedisStore = require('connect-redis')(session)
var filetools = require("./routes/filetools.js")

var routes = require('./routes/index')
var wechat = require('./routes/wechat.js')
var wechat_new = require('./routes/wechat.new.js')
var zmkxj = require('./routes/zmkxj.js')
var wlgs = require('./routes/wlgs.js')
var vote = require('./routes/vote.js')
var rchbsj = require('./routes/rchbsj.js')
var tongueart = require('./routes/tongueart.js')
var jffatherday = require('./routes/jffatherday.js')
var baoli = require('./routes/baoli.js')
var hrth = require('./routes/hrth.js')
var xktfw = require('./routes/xktfw.js')
var qiandao = require('./routes/qiandao.js')
var mongoose = require('mongoose')
var article = require('./routes/wmp/article.js')
var voice = require('./routes/wmp/voice.js')
var getawayfromearth = require('./routes/getawayfromearth.js')
var _static = require('./routes/static.js')
var upload = require('multer')({dest: __dirname + '/public/uploads/'})
var vue_spa = require("./routes/vue_spa.js")
var spa = require("./routes/spa.js")

/***********************************链接到数据库*********************************************/
var uri = 'mongodb://127.0.0.1:26917/myserver'
mongoose.connect(uri, {
    server: {
        poolSize: 10
    },
    auth: {
        user: "tczx",
        pass: "123456Wj"
    }
})
mongoose.connection.on('error', console.error.bind(console, '连接错误:'))

/***********************************链接到数据库*********************************************/
var app = express()

// function clearData() {
//     console.log(new Date());
//     var exec = require('child_process').exec,
//         child;
//     var datapath = path.join(__dirname, "./data/*");
//     child = exec('rm -rf ' + datapath, function (err, out) {
//         if (err) {
//             console.log(err);
//         } else {
//             console.log(out);
//         }
//     });
// }

// 定时任务
// clearData();
// var schedule = require('node-schedule');
// var j = schedule.scheduleJob('0 */2 * * *', function () {
//     clearData();
// });


//设置跨域访问
// app.all('*', function(req, res, next) {
//     res.header("Access-Control-Allow-Origin", "*");
//     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//     res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
//     res.header("X-Powered-By", ' 3.2.1')
//     res.header("Content-Type", "application/json;charset=utf-8");
//     next();
// });

var config = {
    "cookie": {
        "maxAge": 2 * 59 * 60 * 1000
    },
    "sessionStore": {
        "name": "tczx",
        "host": "127.0.0.1",
        "port": "16379",
        "ttl": 30,
        "logErrors": true,
        "pass": 'Ldg0!s123'
    }
}


app.get('/auth/:id/:password', function (req, res) {
    res.send({
        id: req.params.id,
        name: req.params.password
    })
})

// view engine setup
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'jade')

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
    extended: false
}))
app.use(cookieParser())
app.use(session({
    cookie: config.cookie,
    store: new RedisStore(config.sessionStore),
    secret: 'F750M9gJdIXKvZv5'
}))
app.use(express.static(path.join(__dirname, 'public')))
app.use(express.query())
app.use(session({
    secret: 'tczxmyserver',
    resave: false,
    saveUninitialized: true,
    cookie: {secure: true}
}))
app.use('/', wechat)
app.use('/wechat_new', wechat_new)


app.use('/filetools', upload.single('avatar'), filetools)
app.use('/vue_spa', vue_spa)
app.use('/spa', spa)
app.use('/zmkxj', zmkxj)
app.use('/wlgs', wlgs)
app.use('/vote', vote)
app.use('/rchbsj', rchbsj)
app.use('/tongueart', tongueart)
app.use('/jffatherday', jffatherday)
app.use('/baoli', baoli)
app.use('/hrth', hrth)
app.use('/xktfw', xktfw)
app.use('/qiandao', qiandao)
app.use('/wmp/article', article)
app.use('/wmp/voice', voice)
app.use('/getawayfromearth', getawayfromearth)
// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found')
    err.status = 404
    next(err)
})

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500)
        res.render('error', {
            message: err.message,
            error: err
        })
    })
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500)
    res.render('error', {
        message: err.message,
        error: {}
    })
})


module.exports = app